package edu.nuc.ahm.juc.add;

import edu.nuc.ahm.juc.procon.C;

import java.util.concurrent.BrokenBarrierException;
import java.util.concurrent.CyclicBarrier;

public class CyclicBarrrierTest {
    public static void main(String[] args) {
        /*
        * 集齐七颗龙珠召唤神龙
        * */
        // 召唤龙珠的线程

        CyclicBarrier  cyclicBarrier = new CyclicBarrier(7,()->{
            System.out.println("召唤神龙！！！");
        });

        for (int i = 1; i <= 7; i++) {
            final int temp = i;
            // lambda 能操作 i 嘛
            new Thread(()->{
                System.out.println(Thread.currentThread().getName()+"收集了第"+temp+"颗龙珠");

                try {
                    cyclicBarrier.await(); // 每等待一次 +1
                } catch (InterruptedException e) {
                    e.printStackTrace();
                } catch (BrokenBarrierException e) {
                    e.printStackTrace();
                }
            }).start();
        }
    }
}
